package com.it2297.renderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.it2297.model.Product;

public class ProductCellRenderer extends JLabel implements TableCellRenderer  {

	private Product product;
	
	public ProductCellRenderer()
	{
		setOpaque(true);
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		setVerticalTextPosition(JLabel.TOP);
		setIconTextGap(10);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object object, boolean isSelected, boolean hasFocus, int row, int column) {
		product = (Product) object;
		
		switch(column){
		case 0:
			Image bg = Toolkit.getDefaultToolkit().createImage(System.getProperty("user.dir") + "/img/product-img/" + product.getProdName() + ".png").getScaledInstance(100,  70, Image.SCALE_DEFAULT);
			setIcon(new ImageIcon(bg));
			setText("<html><strong> Product-name: " + product.getProdName() + "</strong><br/>Description: " + product.getDesc() +  "<br/>Category: " + product.getCategory() +"</html>");
			break;
		case 1:
			setIcon(null);
			setText("<html><strong> Price: </strong><br/>" + product.getPrice() + "</html>");
			break;
		}
		
		if(row % 2 ==0){
			setBackground(Color.WHITE);
		}else{
			setBackground(new Color(240, 240, 240));
		}
		
		setForeground(Color.BLACK);
		
		if(isSelected){
			setForeground(new Color(255,255,255));
			setBackground(new Color(80,80,80));
		}
		
		return this;
	}

}
